package com.bwda.dsrs.filecenter.controller;

import java.util.List;
import java.util.ArrayList;

import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.annotations.IgnoreClientToken;
import com.bwda.dsrs.filecenter.annotations.NeedRole;
import com.bwda.dsrs.filecenter.domain.condition.clipboardcontrol.ClipboardControlCondition;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.filecenter.domain.form.clipboardcontrol.ClipboardControlCreateForm;
import com.bwda.dsrs.filecenter.domain.form.clipboardcontrol.ClipboardControlQueryForm;
import com.bwda.dsrs.filecenter.domain.form.clipboardcontrol.ClipboardControlUpdateForm;
import com.bwda.dsrs.filecenter.domain.po.clipboardcontrol.ClipboardControlPo;
import com.bwda.dsrs.filecenter.domain.vo.clipboardcontrol.ClipboardControlVo;
import com.bwda.dsrs.filecenter.service.ClipboardControlService;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * @Author system
 * @Descrition todo
 * @Date 2018/6/21
 * @Version 1.0
 * @since jdk1.8
 */
@RestController
@RequestMapping("/b/clipboardcontrol")
@Api(value = "/b/clipboardcontrol", tags = "剪切板监控")
public class ClipboardControlController extends BaseController {

	private int dbLimit1333 = 1333;
	private int dbLimit2666 = 2666;
	private int dbLimit3999 = 3999;

	@Autowired
	private ClipboardControlService clipboardControlService;
	@NeedRole("auditorrole")
	@ApiOperation(value = "查询剪切板监控",notes = "查询剪切板监控",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<ClipboardControlVo> query(@ApiParam(value = "ID", required = true)@RequestParam Integer id) throws BusinessServiceException {
		ClipboardControlPo po = clipboardControlService.queryWithValid(id);
		ClipboardControlVo vo = CopyUtil.transfer(po, ClipboardControlVo.class);
		return getSuccessResult(vo);
	}
	@NeedRole("auditorrole")
	@ApiOperation(value = "查询剪切板监控数量",notes = "查询剪切板监控数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid ClipboardControlQueryForm form) throws BusinessServiceException {
		ClipboardControlCondition condition = this.getConditionByQueryForm(form);
		int count = clipboardControlService.queryCount(condition);
		return getSuccessResult(count);
	}
	@NeedRole("auditorrole")
	@ApiOperation(value = "查询剪切板监控列表",notes = "查询剪切板监控列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<ClipboardControlVo>> queryList(@ModelAttribute@Valid ClipboardControlQueryForm form) throws BusinessServiceException {
		ClipboardControlCondition condition = this.getConditionByQueryForm(form);
		List<ClipboardControlPo> poList = clipboardControlService.queryList(condition);
		List<ClipboardControlVo> voList = CopyUtil.transfer(poList, ClipboardControlVo.class);
		return getSuccessResult(getListResponse(voList));
	}
	@NeedRole("auditorrole")
	@ApiOperation(value = "查询剪切板监控列表(带分页)",notes = "查询剪切板监控列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<ClipboardControlVo>> queryPageList(@ModelAttribute@Valid ClipboardControlQueryForm form) throws BusinessServiceException {
		ClipboardControlCondition condition = this.getConditionByQueryForm(form);
		List<ClipboardControlVo> voList = new ArrayList<>();
		int count = clipboardControlService.queryCount(condition);
		if (count > 0) {
			List<ClipboardControlPo> poList = clipboardControlService.queryList(condition);
			poList.forEach(s -> {
				String content = s.getContent();
				if(s.getContent2()!=null){
					content=content+s.getContent2();
				}
				if (s.getContent3()!=null){
					content=content+s.getContent3();
				}
				s.setContent(content);
			});
			voList = CopyUtil.transfer(poList, ClipboardControlVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增剪切板监控",notes = "新增剪切板监控",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<ClipboardControlVo> add(@ModelAttribute@Valid ClipboardControlCreateForm form) throws BusinessServiceException {
		ClipboardControlPo po = CopyUtil.transfer(form, ClipboardControlPo.class);
		String content = po.getContent();
		//数据库最大长度有限制
		if (content.length()>dbLimit1333&&content.length()<=dbLimit2666){
			po.setContent(content.substring(0,dbLimit1333));
			po.setContent2(content.substring(dbLimit1333,content.length()+1));
		}
		if (content.length()>dbLimit2666&&content.length()<=dbLimit3999){
			po.setContent(content.substring(0,dbLimit1333));
			po.setContent2(content.substring(dbLimit1333,dbLimit2666));
			po.setContent3(content.substring(dbLimit2666,content.length()+1));
		}
		if (content.length()>dbLimit3999){
			po.setContent(content.substring(0,dbLimit1333));
			po.setContent2(content.substring(dbLimit1333,dbLimit2666));
			po.setContent3(content.substring(dbLimit1333,dbLimit3999));
		}
		clipboardControlService.insert(po);
		ClipboardControlVo vo = CopyUtil.transfer(po, ClipboardControlVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改剪切板监控",notes = "修改剪切板监控",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid ClipboardControlUpdateForm form) throws BusinessServiceException {
		ClipboardControlPo po = CopyUtil.transfer(form, ClipboardControlPo.class);
		clipboardControlService.update(po);
		return getSuccessResult();
	}
	@NeedRole("auditorrole")
	@ApiOperation(value = "删除剪切板监控",notes = "删除剪切板监控",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "ID", required = true)@RequestParam Integer id) throws BusinessServiceException {
		clipboardControlService.delete(id);
		return getSuccessResult();
	}

	/**
	 * ClipboardControlQueryForm转换为ClipboardControlCondition
	 *
	 * @param form
	 * @return
	 */
	private ClipboardControlCondition getConditionByQueryForm(ClipboardControlQueryForm form) {
		ClipboardControlCondition condition = CopyUtil.transfer(form, ClipboardControlCondition.class);
		return condition;
	}

}